package ferp.core.state;

import ferp.core.game.Game;
import ferp.core.game.Input;
import ferp.core.game.Restorer;
import ferp.core.game.Settings;
import ferp.core.log.Log;
import ferp.core.mode.Mode;
import ferp.core.player.Profile;

/**
 * User: olegs
 * Date: 2/1/13 12:33 PM
 */
public class Resume extends State
{
  @Override
  public State next() {return next;}
  @Override
  public void next(State next) {this.next = next;}

  @Override
  public int process(Game.Listener listener, Profile profile, Game g, Settings settings, Input input) throws Game.Error
  {
    // restore game state only once
    if (next != null)
    {
      g.set(next);

      next = null;
    }

    Log.debug(Log.TAG, "resuming game, state: " + g.state() + ", mode: " + g.mode());

    // new dealing set (resolves 'DeckManager.getCardFront()' NPE)
    if (g.mode() == Mode.dealing && g.state() == State.initialize)
    {
      listener.onBeforeDeal();

      return Result.SCHEDULE;
    }

    // set WAIT_FOR_RESUMING input option to prevent processing during task execution
    g.options.set(Input.Options.WAIT_FOR_RESUMING);

    // restore the game
    Restorer.execute(listener, g, settings);

    return Result.SHOW_OPTIONS;
  }

  private State next;
}
